package com.qjcc.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qjcc.R;
import com.qjcc.app.AppContext;

/**
 * 应用程序UI工具包：封装UI相关的一些操作
 *
 * @author wangkun
 * @version 1.0
 * @created 2015-9-11
 */
public class UIHelper {

    public static final int CAMERA_REQUEST_CODE = 1001;
    public static final int ALBUM_REQUEST_CODE = 1002;
    public static final int CUT_IMAGE_REQUEST_CODE = 1003;

    /**
     * 弹出Toast消息
     *
     * @param cont
     * @param msg
     */
    public static void ToastMessage(Context cont, String msg) {
        Toast toast = Toast.makeText(cont, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 100);
        toast.show();
    }

    public static void ToastMessage(Context cont, int msg) {
        Toast toast = Toast.makeText(cont, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 100);
        toast.show();
    }

    public static void ToastMessage(Context cont, String msg, int time) {
        Toast toast = Toast.makeText(cont, msg, time);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 100);
        toast.show();
    }

    public static void DialogMessage(Context cont, String title, String message, final ConfirmCallBack confirmCallBack) {
        // 用于子线程与主线程通信的Handler
        final Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                //super.handleMessage(msg);
                // 将返回值回调到callBack的参数中
                if (msg.what == 0) {
                    //确定按钮的事件
                    confirmCallBack.confirmCallBack();
                } else if (msg.what == 1) {
                    //返回按钮的事件
                    confirmCallBack.cancelCallBack();
                }

            }

        };
        new AlertDialog.Builder(cont).setTitle(title)//设置对话框标题
                //.setIcon(android.R.drawable.ic_dialog_info)//设置图标
                .setMessage(message)//设置显示的内容
                .setPositiveButton(AppContext.getAppContext().getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {//添加确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                        mHandler.sendMessage(mHandler.obtainMessage(0, null));
                        //finish();
                    }
                })
                .setNegativeButton(AppContext.getAppContext().getResources().getString(R.string.back), new DialogInterface.OnClickListener() {//添加返回按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//响应事件
                        mHandler.sendMessage(mHandler.obtainMessage(1, null));
                        //Log.i("alertdialog"," 请保存数据！");
                    }
                })
                .show();//在按键响应事件中显示此对话框
    }

    /**
     * 点击返回监听事件,用于关闭当前Activity
     *
     * @param activity
     * @return
     */
    public static View.OnClickListener finish(final Activity activity) {
        return new View.OnClickListener() {
            public void onClick(View v) {
                activity.finish();
            }
        };
    }

    /**
     * 点击“确定”返回接口
     */
    public interface ConfirmCallBack {
        public void confirmCallBack();

        public void cancelCallBack();
    }

    /**
     * 带输入框对话框点击按钮回调接口
     */
    public interface InputDialogCallback {
        public void confirmCallBack(String value);

        public void cancelCallBack();

    }

    /**
     * 获取头像回调接口
     */
    public interface PhotoCallback {
        /**
         * 相机照片uri
         *
         * @param uri
         */
        public void cameraUri(Uri uri);
    }

    /**
     * 设置列表项的是否可点击
     * @param view
     * @param clicked
     */
    public static void setItemClicked(View view, boolean clicked) {
        if (view != null) {
            try {
                ViewGroup viewGroup = (ViewGroup) view.getParent();
                View v = viewGroup.getChildAt(2);
                if (clicked) {
                    v.setVisibility(View.VISIBLE);
                } else {
                    v.setVisibility(View.INVISIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取列表项是否可点击
     * @param view
     * @return
     */
    public static boolean getItemClicked(View view) {
        boolean clicked = false;
        try {
            ViewGroup viewGroup = (ViewGroup) view;
            if (viewGroup != null) {
                View v = viewGroup.getChildAt(2);
                if (v.getVisibility() == View.VISIBLE) {
                    clicked = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clicked;
    }

}
